package service

import (
	"context"
	"iris-wms/apps/admin/web/dto/din"
	"iris-wms/apps/admin/web/dto/dout"
	"iris-wms/common/model"
	"iris-wms/common/pkg/db"
	"iris-wms/common/pkg/db/dao"

	"gorm.io/gorm"
)

var _ AdminButtonService = (*adminButtonService)(nil)

type AdminButtonService interface {
	Create(*model.AdminButton) error
	Update(*model.AdminButton) error
	DeleteByIdList([]uint) error
	Generate(uint) error
	GetTable(din.VxeTableIn) ([]*dout.AdminButtonOut, int64, error)
	GetList() ([]*dout.AdminButtonOut, error)
}

func NewAdminButtonService() *adminButtonService {
	return &adminButtonService{}
}

type adminButtonService struct{}

func (impl *adminButtonService) Create(in *model.AdminButton) (err error) {
	err = db.GDB1.Create(&in).Error
	return
}

func (impl *adminButtonService) Update(in *model.AdminButton) (err error) {
	err = db.GDB1.Save(&in).Error
	return
}

func (impl *adminButtonService) DeleteByIdList(idList []uint) (err error) {
	err = db.GDB1.Transaction(func(tx *gorm.DB) error {
		if err = tx.Delete(&model.AdminButton{}, "id in ?", idList).Error; err != nil {
			return err
		}
		if err = tx.Delete(&model.AdminMenuButton{}, "bid in ?", idList).Error; err != nil {
			return err
		}
		if err = tx.Model(&model.AdminRoleMenu{}).Where("bid in ?", idList).Update("bid", 0).Error; err != nil {
			return err
		}
		return nil
	})
	return
}

func (impl *adminButtonService) Generate(uid uint) (err error) {
	// 系统内置的默认按钮
	var list = [...]model.AdminButton{
		{Model: gorm.Model{ID: 1}, Uid: uid, Name: "刷新表", Place: model.Top_Right},
		{Model: gorm.Model{ID: 2}, Uid: uid, Name: "导入", Place: model.Top_Right},
		{Model: gorm.Model{ID: 3}, Uid: uid, Name: "导出", Place: model.Top_Right},
		{Model: gorm.Model{ID: 4}, Uid: uid, Name: "打印", Place: model.Top_Right},
		{Model: gorm.Model{ID: 5}, Uid: uid, Name: "最大化", Place: model.Top_Right},
		{Model: gorm.Model{ID: 6}, Uid: uid, Name: "自定义列", Place: model.Top_Right},
		//
		{Model: gorm.Model{ID: 7}, Uid: uid, Name: "创建", Place: model.Top_Left, Func: "create"},
		{Model: gorm.Model{ID: 8}, Uid: uid, Name: "编辑", Place: model.Table_Inner, Func: "edit"},
		{Model: gorm.Model{ID: 9}, Uid: uid, Name: "删除", Place: model.Table_Inner, Func: "deletes", Tip: 1, TipContent: "警告：数据删除后不可恢复！确定删除吗？"},
		{Model: gorm.Model{ID: 10}, Uid: uid, Name: "复制行", Place: model.Table_Inner, Func: "copy"},
		{Model: gorm.Model{ID: 11}, Uid: uid, Name: "授权", Place: model.Table_Inner, Func: "grant"},
		{Model: gorm.Model{ID: 12}, Uid: uid, Name: "重置密码", Place: model.Table_Inner, Func: "resetPassword"},
		// wms bill
		{Model: gorm.Model{ID: 13}, Uid: uid, Name: "创建入库单", Place: model.Table_Inner},
		{Model: gorm.Model{ID: 14}, Uid: uid, Name: "创建出库单", Place: model.Table_Inner},
		{Model: gorm.Model{ID: 15}, Uid: uid, Name: "创建移库单", Place: model.Table_Inner},
		// wms warehouse
		{Model: gorm.Model{ID: 16}, Uid: uid, Name: "创建仓库", Place: model.Other},
		{Model: gorm.Model{ID: 17}, Uid: uid, Name: "创建库区", Place: model.Other},
		{Model: gorm.Model{ID: 18}, Uid: uid, Name: "修改", Place: model.Other},
	}

	err = db.GDB1.Transaction(func(tx *gorm.DB) error {
		for _, v := range list {
			err := tx.Where(model.AdminButton{Model: gorm.Model{ID: v.ID}}).FirstOrCreate(&v).Error
			if err != nil {
				return err
			}
		}
		return nil
	})
	return
}

func (impl *adminButtonService) GetTable(in din.VxeTableIn) (rows []*dout.AdminButtonOut, total int64, err error) {
	q := dao.Use(db.GDB1).AdminButtonOut
	rows, total, err = q.WithContext(context.Background()).
		Order(q.Place, q.ID).
		Preload(q.Creator).
		FindByPage(in.Offset(), in.PageSize)
	return
}

func (impl *adminButtonService) GetList() (rows []*dout.AdminButtonOut, err error) {
	err = db.GDB1.Find(&rows).Error
	return
}
